package com.back.service.impl;

import com.back.excepction.BaseException;
import com.back.model.entity.GroupUser;
import com.back.mapper.GroupUserMapper;
import com.back.model.dto.GroupcDTO;
import com.back.model.entity.User;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.back.model.entity.Groupc;
import com.back.service.GroupcService;
import com.back.mapper.GroupcMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
* @author Administrator
* @description 针对表【groupc(班级)】的数据库操作Service实现
* @createDate 2025-09-11 10:33:00
*/
@Service
public class GroupcServiceImpl extends ServiceImpl<GroupcMapper, Groupc>
    implements GroupcService{

    @Resource
    private GroupUserMapper groupUserMapper;

    @Override
    public Groupc createGroup(GroupcDTO groupcDTO) {
        Groupc groupc = new Groupc();
        BeanUtils.copyProperties(groupcDTO,groupc);
        baseMapper.insert(groupc);
        return groupc;
    }

    @Override
    public Groupc joinGroup(GroupcDTO groupcDTO) {
        Groupc groupc = baseMapper.selectOne(new LambdaQueryWrapper<Groupc>().eq(Groupc::getGroupNo, groupcDTO.getGroupNo()));
        if(groupc == null){
            throw new BaseException("班级号不存在");
        }

        LambdaQueryWrapper<GroupUser> lambdaQueryWrapper = new QueryWrapper<GroupUser>().lambda();
        lambdaQueryWrapper.eq(GroupUser::getUserId, groupcDTO.getUserId()).eq(GroupUser::getGroupId, groupc.getId());
        GroupUser groupUser = groupUserMapper.selectOne(lambdaQueryWrapper);
        if(groupUser != null){
            throw new BaseException("已经加入 不能重复加入");
        }
        BeanUtils.copyProperties(groupcDTO,groupUser);
        groupUser.setGroupId(groupc.getId());
        groupUserMapper.insert(groupUser);
        return null;
    }

    @Override
    public int exitGroup(GroupcDTO groupcDTO) {
        LambdaQueryWrapper<GroupUser> lambdaQueryWrapper = new QueryWrapper<GroupUser>().lambda();
        lambdaQueryWrapper.eq(GroupUser::getUserId, groupcDTO.getUserId()).eq(GroupUser::getGroupId, groupcDTO.getId());
        return groupUserMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public List<User> getGroupUser(GroupcDTO groupcDTO) {
        return Collections.emptyList();
    }
}




